<?php

//装载模板文件
include_once("wx_tpl.php");
include_once("base-class.php");

//新建sae数据库类
$mysql = new SaeMysql();

//新建Memcache类
$mc=memcache_init();

//获取微信发送数据
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

//操作菜单
$help_menu="请回复序号：\n1.积分查询   2.联系方式\n3.积分明细   4.项目列表\n5.添加积分   6.审核积分\n7.管理入口\n\n0.查看帮助\n\n更多功能敬请期待……";

//在职状态
$roster_status_ary=array('','在职','休假','病假','离职');

  //返回回复数据
if (!empty($postStr))
{
          
    //解析数据
    $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
    //发送消息方ID
    $fromUsername = $postObj->FromUserName;
    //接收消息方ID
    $toUsername = $postObj->ToUserName;
    //消息类型
    $form_MsgType = $postObj->MsgType;
    
    //欢迎消息
    if($form_MsgType=="event")
    {
        //获取事件类型
        $form_Event = $postObj->Event;
        //订阅事件
        if($form_Event=="subscribe")
        {
            
            
            //根据关注用户的OPENID判断是否已经绑定员工号
            if(check_user($fromUsername))
            {
                
                //关注绑定欢迎词
                $welcome_str="感谢您关注泰思曼积分公众平台！\n\n".$help_menu;
                
            }
            else
            {
                
                //关注未绑定欢迎词
                $welcome_str="感谢您关注泰思曼积分公众平台！[愉快]\n\n本平台是内部平台，请回复“BD”开始绑定！管理员：小涛。谢谢！[玫瑰]";
            }
            //回复欢迎文字消息
            $msgType = "text";
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, $welcome_str);
            echo $resultStr;
            exit;  
        }
    }
    
    //用户文字回复进行绑定、查询等操作
    
    if($form_MsgType=="text")
    {
        //获取用户发送的文字内容并过滤
        $form_Content = trim($postObj->Content);
        $form_Content = string::un_script_code($form_Content);
        
        
        //如果发送内容不是空白则执行相应操作
        //if(!empty($form_Content))
        if(1)
        {
            //从memcache获取用户上一次动作
            $last_do=$mc->get($fromUsername."_do");
            //从memcache获取用户上一次数据
            $last_data=$mc->get($fromUsername."_data");
            
            $nowtime=date("Y/m/d H:i:s",time());
            
            //用户帮助提示
            if((strtolower($form_Content)=="help")||(strtolower($form_Content)=="0"))
            {
                //清空memcache动作
                $mc->delete($fromUsername."_do");
                
                //清空memcache数据
                $mc->delete($fromUsername."_data");
                //根据关注用户的OPENID判断是否已经绑定员工号
                if(check_user($fromUsername))
                {
                    
                    //关注绑定欢迎词
                    $help_str="泰思曼积分平台V1.5使用帮助：\n\n".$help_menu;
                    
                }
                else
                {
                    
                    //关注未绑定欢迎词
                    $help_str="你还未绑定微信账号，不能使用泰思曼积分平台，请回复“BD”开始绑定！";
                }
                //回复文字消息
                $msgType = "text";
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, $help_str);
                echo $resultStr;
                exit;  
            }
            //用户跳出操作
            if(strtolower($form_Content)=="exit")
            {
                //清空memcache动作
                $mc->delete($fromUsername."_do");
                
                //清空memcache数据
                $mc->delete($fromUsername."_data");
                
                //回复操作提示
                $msgType = "text";
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "你已经退出当前操作，进行其他操作请输入“help”");
                echo $resultStr;
                exit;  
            }
            
            
           
            //积分搜索
            if((strtolower($form_Content)=="1")&&(!$last_do))
            {
                //用memcache保存部门搜索操作状态
                $mc->set($fromUsername."_do", "jf_search", 0, 600);
                
                $roster_value=$mysql->getLine("select roster_name,roster_id from roster where roster_openid='$fromUsername'");
                $rmb_value_new=$mysql->getVar("select sum(rmb_rmb)*DATE_FORMAT( NOW( ),'%d')/DAYOFMONTH(last_day(curdate())) from rmb where `rmb_addtime` > DATE_FORMAT( NOW( ) ,  '%Y-%m-01' )  and rmb_rmb>0");//当月新增奖金平滑
                $rmb_value_old=$mysql->getVar("select sum(rmb_rmb)from rmb where `rmb_addtime` < DATE_FORMAT( NOW( ) ,  '%Y-%m-01' )  or rmb_rmb<0");//历史奖金+兑换奖金                
                $rmb_value = $rmb_value_new + $rmb_value_old;
                $list_allsum_value=$mysql->getVar("select sum(list_money) from list where list_status = 1 "); //查询总积分
                $list_value=$mysql->getVar("select sum(list_money) from list,roster where list_name = roster_name and 
                							roster_openid='$fromUsername' and list_status = 1");//查询个人积分                
                //回复操作提示
                $name_str = "您的积分信息如下：\n\n姓名：".$roster_value["roster_name"]."\n积分：".$list_value."\n实时汇率：".round($rmb_value/$list_allsum_value,3)."\n积分价值：".
                    round($list_value*$rmb_value/$list_allsum_value)."元\n税后：".round($list_value*$rmb_value*0.9/$list_allsum_value).
                    "元\n<a href=\"http://teslamoney.vipsinaapp.com/list_list.php?id=".$roster_value["roster_id"]."\">查看明细</a>\n\n继续查询请输入员工姓名或拼音首字母查询（如“张三”请输入“ZS”）：";
                $msgType = "text";
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, $name_str);
                echo $resultStr;
                exit;  
            }
            //接收到上一步是工号搜索状态，获取用户查询关键字
            if($last_do=="jf_search")
            {
                //查询数据库
                $list_value=$mysql->getVar("select sum(list_money) from list,roster where list_name = roster_name and 
                							(list_name='$form_Content' or roster_py='$form_Content' )and list_status = 1");//查询个人积分
                $rmb_value_new=$mysql->getVar("select sum(rmb_rmb)*DATE_FORMAT( NOW( ),'%d')/DAYOFMONTH(last_day(curdate())) from rmb where `rmb_addtime` > DATE_FORMAT( NOW( ) ,  '%Y-%m-01' )  and rmb_rmb>0");//当月新增奖金平滑
                $rmb_value_old=$mysql->getVar("select sum(rmb_rmb)from rmb where `rmb_addtime` < DATE_FORMAT( NOW( ) ,  '%Y-%m-01' )  or rmb_rmb<0");//历史奖金+兑换奖金                
                $rmb_value = $rmb_value_new + $rmb_value_old;
                $list_allsum_value=$mysql->getVar("select sum(list_money) from list where list_status = 1"); //查询总奖金额 
                $roster_value=$mysql->getLine("select roster_name,roster_id from roster where roster_py='$form_Content' or roster_name='$form_Content'"); 
                //查无此人
                if(!$roster_value)
                {
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "无此员工，请重新输入，或者输入“0”返回主菜单！");
                    echo $resultStr;
                    exit;  
                }
                //积分信息
                else
                {
                    //用文字消息输出
                    
                $name_str = "您的积分信息如下：\n\n姓名：".$roster_value["roster_name"]."\n积分：".$list_value."\n汇率：".round($rmb_value/$list_allsum_value,3)."\n积分价值：".
                    round($list_value*$rmb_value/$list_allsum_value)."元\n税后：".round($list_value*$rmb_value*0.9/$list_allsum_value).
                    "元\n<a href=\"http://teslamoney.vipsinaapp.com/list_list.php?id=".$roster_value["roster_id"]."\">查看明细</a>\n\n继续查询请输入员工姓名或拼音首字母查询（如“张三”请输入“ZS”）：";
                $msgType = "text";
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, $name_str);
                echo $resultStr;
                exit;  
                }
                
            }
            
            //通讯录查询
            if((strtolower($form_Content)=="2")&&(!$last_do))
            {
                //用memcache保存姓名搜索操作状态
                $mc->set($fromUsername."_do", "txl_search", 0, 600);
                
                //回复操作提示
                $msgType = "text";
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "请输入员工姓名或拼音首字母查询（如“张三”请输入“ZS”）：");
                echo $resultStr;
                exit;  
            }
            //接收到上一步是姓名搜索状态，获取用户查询关键字
            if($last_do=="txl_search")
            {
                //查询数据库
                $roster_value=$mysql->getLine("select roster_id,roster_name,roster_number,roster_status,roster_mp,roster_mail,roster_phone
                    							   from roster
                                                   where roster_name='$form_Content' or roster_py='$form_Content' ");
                //查无此人
                if(!$roster_value)
                {
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "无此员工，请重新输入，或者输入“0”返回主菜单！");
                    echo $resultStr;
                    exit;  
                }
                //查询成功返回图文消息
                else
                {
                    //用文字消息输出员工信息
                    // $back_str="姓名：""感谢您关注泰思曼积分公众平台！[愉快]\n\n公众平台正在建设中，有任何问题欢迎反馈，管理员：小涛。谢谢！[玫瑰]";
                    
                    //回复欢迎文字消息
                    $name_str = "姓名：".$roster_value["roster_name"]."\n工号：".$roster_value["roster_number"].
                        "\n手机：".$roster_value["roster_mp"]."\n分机：".$roster_value["roster_phone"]."\n邮箱：".$roster_value["roster_mail"];
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, $name_str);
                    echo $resultStr;
                    exit;  
                }
                
            }  
            
            if((strtolower($form_Content)=="3")&&(!$last_do))
            {

                //用图文消息输出明细信息
                //$money_value=$mysql->getVar("select sum(list_money) from list where list_name='$form_Content' and list_status = 1");//查询个人积分
                    $resultStr="<xml>\n
                                    <ToUserName><![CDATA[".$fromUsername."]]></ToUserName>\n
                                    <FromUserName><![CDATA[".$toUsername."]]></FromUserName>\n
                                    <CreateTime>".time()."</CreateTime>\n
                                    <MsgType><![CDATA[news]]></MsgType>\n
                                    <ArticleCount>1</ArticleCount>\n
                                    <Articles>\n
                                    <item>\n
                                    <Title><![CDATA[".'积分明细一览表'."]]></Title> \n
                                    <Description><![CDATA[积分明细请点击阅读全文！]]></Description>\n
                                    <PicUrl><![CDATA[".'http://ww3.sinaimg.cn/bmiddle/3efed9cctw1eha0jyg3agj20go0bhtb4.jpg'."]]></PicUrl>\n
                                    <Url><![CDATA[".'http://teslamoney.vipsinaapp.com/list_top10.php'."]]></Url>\n
                                    </item>\n
                                    </Articles>\n
                                    <FuncFlag>0</FuncFlag>\n
                                    </xml>";
                    echo $resultStr;
                    exit;
            }                
            
            
            if((strtolower($form_Content)=="4")&&(!$last_do))
            {
                
                
                //用图文消息输出积分信息
                //$money_value=$mysql->getVar("select sum(list_money) from list where list_name='$form_Content' and list_status = 1");//查询个人积分
                    $resultStr="<xml>\n
                                    <ToUserName><![CDATA[".$fromUsername."]]></ToUserName>\n
                                    <FromUserName><![CDATA[".$toUsername."]]></FromUserName>\n
                                    <CreateTime>".time()."</CreateTime>\n
                                    <MsgType><![CDATA[news]]></MsgType>\n
                                    <ArticleCount>1</ArticleCount>\n
                                    <Articles>\n
                                    <item>\n
                                    <Title><![CDATA[".'积分项目一览表'."]]></Title> \n
                                    <Description><![CDATA[积分项目请点击阅读全文！]]></Description>\n
                                    <PicUrl><![CDATA[".'http://ww3.sinaimg.cn/bmiddle/3efed9cctw1eha0jyg3agj20go0bhtb4.jpg'."]]></PicUrl>\n
                                    <Url><![CDATA[".'http://teslamoney.vipsinaapp.com/gift_list.php'."]]></Url>\n
                                    </item>\n
                                    </Articles>\n
                                    <FuncFlag>0</FuncFlag>\n
                                    </xml>";
                    echo $resultStr;
                    exit;
            }               

            
            //添加积分项
            if((strtolower($form_Content)=="5")&&(!$last_do))
            {
                
                //用memcache保存这步操作，格式为名称、值、有效时间(单位秒)；
                $mc->set($fromUsername."_do", "tj_0", 0, 600);
                //回复绑定下一步操作提示
                $msgType = "text";
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "请输入姓名或拼音首字母（如“张三”请输入“ZS”）：\n输入“0”返回主菜单！");
                echo $resultStr;
                exit;  
            }
            
            //添加积分项第一步，输入姓名   
            if($last_do=="tj_0")
            {
                
                $roster_name_tj=$mysql->getVar("select roster_name
                    							   from roster
                                                   where roster_name='$form_Content' or roster_py='$form_Content'");
                //查无此人
                if(!$roster_name_tj)
                {
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "无此员工，请重新输入，或者输入“0”返回主菜单！");
                    echo $resultStr;
                    exit;  
                }
                else
                {
                    
                    //用memcache保存这步操作
                    $mc->set($fromUsername."_do", "tj_1", 0, 600);
                    
                    //用memcache保存这步数据
                    $mc->set($fromUsername."_data", $roster_name_tj, 0, 3600);
                    
                    
                    $back_str = "您要为".$roster_name_tj."添加积分"."\n请输入积分代码：\n".
                        "\n<a href=\"http://teslamoney.vipsinaapp.com/gift_list.php\">积分项目一览表</a>";
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, $back_str);
                    echo $resultStr;
                    exit;  
                    

                }
            }            
            
            //添加积分项第二步，输入积分代码 
            if($last_do=="tj_1")
            {
                
                $gift_value=$mysql->getLine("select gift_code,gift_item,gift_score
                   							   from gift
                                               where gift_code='$form_Content'");                   
                
                //查无
                if(!$gift_value)
                {
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "无此积分项请重新输入：或者输入“0”返回主菜单！");
                    echo $resultStr;
                    exit;  
                }
                
                //用memcache保存这步操作
                $mc->set($fromUsername."_do", "tj_2", 0, 600);
                
                //用memcache保存这步数据，同时与上次数据合并，两个数据之间用||分割
                $mc->set($fromUsername."_data", $last_data."||".$gift_value["gift_code"], 0, 3600);

                list($roster_name,$gift_code)=explode("||",$last_data."||".$form_Content);                
                $gift_value=$mysql->getLine("select gift_code,gift_item,gift_score,gift_dynamic
                   							   from gift
                                               where gift_code='$gift_code'");      
                
                $rmb_value_new=$mysql->getVar("select sum(rmb_rmb)*DATE_FORMAT( NOW( ),'%d')/DAYOFMONTH(last_day(curdate())) from rmb where `rmb_addtime` > DATE_FORMAT( NOW( ) ,  '%Y-%m-01' )  and rmb_rmb>0");//当月新增奖金平滑
                $rmb_value_old=$mysql->getVar("select sum(rmb_rmb)from rmb where `rmb_addtime` < DATE_FORMAT( NOW( ) ,  '%Y-%m-01' )  or rmb_rmb<0");//历史奖金+兑换奖金                
                $rmb_value = $rmb_value_new + $rmb_value_old;
                $list_allsum_value=$mysql->getVar("select sum(list_money) from list where list_status = 1 "); //查询总积分
                $exchange_rate = $rmb_value/$list_allsum_value; //计算汇率
                
                //回复绑定下一步操作提示
                if($gift_value["gift_dynamic"]==0)  //如果是非动态积分
                {
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "请输入您要申请的分值：\n积分项目：".$gift_value["gift_item"]."\n当前分值：".$gift_value["gift_score"]."分");
                    echo $resultStr;
                    exit;  
                }
                else if($gift_value["gift_dynamic"]==1)
                {
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "请输入您要申请的分值：\n积分项目：".$gift_value["gift_item"]."\n当前分值：".round($gift_value["gift_score"]/$exchange_rate,1)."分");
                    echo $resultStr;
                    exit;                  
                }
            }    
            
            //添加积分项第三步，输入积分分值
            if($last_do=="tj_2")
            {
                
                //用memcache保存这步操作
                $mc->set($fromUsername."_do", "tj_3", 0, 600);
                
                //用memcache保存这步数据，同时与上次数据合并，两个数据之间用||分割
                $mc->set($fromUsername."_data", $last_data."||".$form_Content, 0, 3600);
                
                list($roster_name,$gift_code,$list_money)=explode("||",$last_data."||".$form_Content);
                
                $gift_value=$mysql->getLine("select gift_code,gift_item,gift_score,gift_dynamic
                   							   from gift
                                               where gift_code='$gift_code'");      
                
                $rmb_value_new=$mysql->getVar("select sum(rmb_rmb)*DATE_FORMAT( NOW( ),'%d')/DAYOFMONTH(last_day(curdate())) from rmb where `rmb_addtime` > DATE_FORMAT( NOW( ) ,  '%Y-%m-01' )  and rmb_rmb>0");//当月新增奖金平滑
                $rmb_value_old=$mysql->getVar("select sum(rmb_rmb)from rmb where `rmb_addtime` < DATE_FORMAT( NOW( ) ,  '%Y-%m-01' )  or rmb_rmb<0");//历史奖金+兑换奖金                
                $rmb_value = $rmb_value_new + $rmb_value_old;
                $list_allsum_value=$mysql->getVar("select sum(list_money) from list where list_status = 1 "); //查询总积分
                $exchange_rate = $rmb_value/$list_allsum_value; //计算汇率
                
                //回复绑定下一步操作提示
                if($gift_value["gift_dynamic"]==0)  //如果是非动态积分
                {
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "您要添加的积分信息：\n积分人：".$roster_name."\n积分项目：".$gift_value["gift_item"]."（".$gift_value["gift_score"]."分）"."\n申请积分：".$list_money."\n确认添加输入“1”输入“0”返回主菜单！");
                    echo $resultStr;
                    exit;  
                }
                else if($gift_value["gift_dynamic"]==1)
                {
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "您要添加的积分信息：\n积分人：".$roster_name."\n积分项目：".$gift_value["gift_item"]."（".round($gift_value["gift_score"]/$exchange_rate,1)."分）"."\n申请积分：".$list_money."\n确认添加输入“1”输入“0”返回主菜单！");
                    echo $resultStr;
                    exit;                  
                }
            }                 
            
            //添加积分项第四步，确认添加
            if($last_do=="tj_3")
            {
                
                if(strtolower($form_Content)=="1")
                {
                    list($roster_name,$gift_code,$list_money)=explode("||",$last_data);
                    
                    //清空memcache动作
                    $mc->delete($fromUsername."_do");
                    
                    //清空memcache数据
                    $mc->delete($fromUsername."_data");
                    
                    //新增
                    $sql = "insert into list 
                               (list_name,list_code,list_money,list_addtime,list_status)
                               values 
                               ('$roster_name','$gift_code','$list_money','$nowtime','0')";
                    
                    $mysql->runSql( $sql );
                    
                    if( $mysql->errno() != 0 )
                    {
                        $msgType = "text";
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "错误代码：".$mysql->errno());
                        echo $resultStr;
                        
                        exit;  
                    } 
                    else
                    {
                        $msgType = "text";
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "添加成功！");
                        echo $resultStr;
                        
                        exit;                         
                    }
                    
                    
                }
                else   
                {
                    //清空memcache动作
                    $mc->delete($fromUsername."_do");
                    
                    //清空memcache数据
                    $mc->delete($fromUsername."_data");
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "你已经退出当前操作，进行其他操作请输入“0”");
                    echo $resultStr;
                    exit;  
                }    
            }                      
            
            //审核
            if((strtolower($form_Content)=="6")&&(!$last_do))
            {
                
   
                $roster_check=$mysql->getLine("select roster_S,roster_Y,roster_Q,roster_M,roster_C,roster_K,roster_X
                   							   from roster
                                               where roster_openid='$fromUsername'");       
                
                //有审核权限
                if(($roster_check["roster_S"])||($roster_check["roster_Y"])||($roster_check["roster_Q"])||($roster_check["roster_M"])||
                   ($roster_check["roster_C"])||($roster_check["roster_K"])||($roster_check["roster_X"]))
                {
                    
                    $roster_id=$mysql->getVar("select roster_id
                   							   from roster
                                               where roster_openid='$fromUsername'");    
                    
                    //用memcache保存这步操作，格式为名称、值、有效时间(单位秒)；
                    $mc->set($fromUsername."_do", "sh_0", 0, 600);
                    
                    $resultStr="<xml>\n
                                    <ToUserName><![CDATA[".$fromUsername."]]></ToUserName>\n
                                    <FromUserName><![CDATA[".$toUsername."]]></FromUserName>\n
                                    <CreateTime>".time()."</CreateTime>\n
                                    <MsgType><![CDATA[news]]></MsgType>\n
                                    <ArticleCount>1</ArticleCount>\n
                                    <Articles>\n
                                    <item>\n
                                    <Title><![CDATA[".'请输入您要审核的积分序号'."]]></Title> \n
                                    <Description><![CDATA[查看积分序号请点击阅读全文！]]></Description>\n
                                    <PicUrl><![CDATA[".$roster_value["roster_pic"]."]]></PicUrl>\n
                                    <Url><![CDATA[".'http://teslamoney.vipsinaapp.com/check_list.php?id='.$roster_id."]]></Url>\n
                                    </item>\n
                                    </Articles>\n
                                    <FuncFlag>0</FuncFlag>\n
                                    </xml>";
                    echo $resultStr;
                    exit;
                    
                }
                else//无审核权限
                {
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "对不起您无审核权限。\n\n".$help_menu);
                    echo $resultStr;
                    exit;  
                }
            }
            
            
            //审核
            if($last_do=="sh_0")
            {
                $roster_check=$mysql->getLine("select roster_S,roster_Y,roster_Q,roster_M,roster_C,roster_K,roster_X,roster_name from roster where roster_openid = '$fromUsername' ");
                $list_id=$mysql->getVar("select list_id
                                      from list where 
                                      (( list_code REGEXP '^S' and $roster_check[roster_S] ='1' )
                                      or( list_code REGEXP '^Y' and $roster_check[roster_Y] ='1' )
                                      or( list_code REGEXP '^Q' and $roster_check[roster_Q] ='1' )
                                      or( list_code REGEXP '^M' and $roster_check[roster_M] ='1' )
                                      or( list_code REGEXP '^C' and $roster_check[roster_C] ='1' )
                                      or( list_code REGEXP '^K' and $roster_check[roster_K] ='1' )
                                      or( list_code REGEXP '^X' and $roster_check[roster_X] ='1' ))
                                      and list_status='0' and list_id='$form_Content'");
                if(!$list_id)
                {
                    //无此积分项
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "无此积分项，请重新输入！".$list_id);
                    echo $resultStr;
                    exit;                    
                }
                else
                {
                    
                    $checker = $roster_check["roster_name"];
                    
                    $sql = "update list set `list_checkname`='$checker',list_checktime='$nowtime',list_status='1' where list_id='$list_id'";
                    $mysql->runSql( $sql );
                    
                    if( $mysql->errno() != 0 )
                    {
                        $msgType = "text";
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "错误代码：".$mysql->errno());
                        echo $resultStr;
                        
                        exit;  
                        
                    }
                    else
                    {
                        $msgType = "text";
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "添加成功！");
                        echo $resultStr;
                        
                        exit;         
                    }
                }
            }  
            
            //7####管理入口
            if((strtolower($form_Content)=="7")&&(!$last_do))
            {
                
                //用memcache保存这步操作，格式为名称、值、有效时间(单位秒)；
                $mc->set($fromUsername."_do", "gl_0", 0, 600);
                //回复绑定下一步操作提示
                $msgType = "text";
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "请回复序号：\n1.删除积分   2.添加奖金\n3.积分兑换\n\n0.返回首页");
                echo $resultStr;
                exit;  
            }            
            

            
            //删除积分
            if((strtolower($form_Content)=="1")&&($last_do=="gl_0"))
            {
                
   
                $roster_check=$mysql->getLine("select roster_S,roster_Y,roster_Q,roster_M,roster_C,roster_K,roster_X
                   							   from roster
                                               where roster_openid='$fromUsername'");       
                
                //有删除权限
                if(($roster_check["roster_S"])||($roster_check["roster_Y"])||($roster_check["roster_Q"])||($roster_check["roster_M"])||
                   ($roster_check["roster_C"])||($roster_check["roster_K"])||($roster_check["roster_X"]))
                {
                    
                    $roster_id=$mysql->getVar("select roster_id
                   							   from roster
                                               where roster_openid='$fromUsername'");    
                    
                    //用memcache保存这步操作，格式为名称、值、有效时间(单位秒)；
                    $mc->set($fromUsername."_do", "gl_jfsc_0", 0, 600);
                    
                	$msgType = "text";
                	$resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "请输入您要删除的积分序号：\n\n<a href=\"http://teslamoney.vipsinaapp.com/check_list.php?id=".$roster_id."\">待审核积分</a>");
                    echo $resultStr;
                    exit;
                    
                }
                else//无删除权限
                {
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "对不起您无审核权限。\n\n".$help_menu);
                    echo $resultStr;
                    exit;  
                }
            }
                        
            //删除积分
            if($last_do=="gl_jfsc_0")
            {
                $roster_check=$mysql->getLine("select roster_S,roster_Y,roster_Q,roster_M,roster_C,roster_K,roster_X,roster_name from roster where roster_openid = '$fromUsername' ");
                $list_id=$mysql->getVar("select list_id
                                      from list where 
                                      (( list_code REGEXP '^S' and $roster_check[roster_S] ='1' )
                                      or( list_code REGEXP '^Y' and $roster_check[roster_Y] ='1' )
                                      or( list_code REGEXP '^Q' and $roster_check[roster_Q] ='1' )
                                      or( list_code REGEXP '^M' and $roster_check[roster_M] ='1' )
                                      or( list_code REGEXP '^C' and $roster_check[roster_C] ='1' )
                                      or( list_code REGEXP '^K' and $roster_check[roster_K] ='1' )
                                      or( list_code REGEXP '^X' and $roster_check[roster_X] ='1' ))
                                      and list_status='0' and list_id='$form_Content'");
                if(!$list_id)
                {
                    //无此积分项
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "无此积分项，请重新输入！".$list_id);
                    echo $resultStr;
                    exit;                    
                }
                else
                {
                    
                    $checker = $roster_check["roster_name"];
                    
                    $sql = "update list set `list_checkname`='$checker',list_checktime='$nowtime',list_status='2' where list_id='$list_id'";
                    $mysql->runSql( $sql );
                    
                    if( $mysql->errno() != 0 )
                    {
                        $msgType = "text";
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "错误代码：".$mysql->errno());
                        echo $resultStr;
                        
                        exit;  
                        
                    }
                    else
                    {
                        $msgType = "text";
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "删除成功！");
                        echo $resultStr;
                        
                        exit;         
                    }
                }
            }              
            //添加RMB第一步，检查权限
            if((strtolower($form_Content)=="2")&&($last_do=="gl_0"))
            {

                $roster_name=$mysql->getVar("select roster_name
                    							   from roster
                                                   where roster_openid = '$fromUsername'and roster_K = 1");
                //无权限添加
                if(!$roster_name)
                {
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "对不起，您无此权限，输入“0”返回主菜单！");
                    echo $resultStr;
                    exit;  
                }
                else
                {
                    
                    //用memcache保存这步操作
                    $mc->set($fromUsername."_do", "gl_tjRMB_0", 0, 600);
                    
                    //用memcache保存这步数据
                    $mc->set($fromUsername."_data", $roster_name, 0, 3600);
                    $rmb_value=$mysql->getVar("select sum(rmb_rmb) from rmb ");//当月新增奖金平滑
                
                    
                    //回复下一步操作提示                    
                    $back_str = "当前奖池总金额为：".$rmb_value."\n请输入奖金人民币金额：";
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, $back_str);
                    echo $resultStr;
                    exit;  
                    

                }
            }            
            
            //添加RMB第二步，输入金额
            if($last_do=="gl_tjRMB_0")
            {
                
                //用memcache保存这步操作
                $mc->set($fromUsername."_do", "gl_tjRMB_1", 0, 600);
                
                //用memcache保存这步数据，同时与上次数据合并，两个数据之间用||分割
                $mc->set($fromUsername."_data", $last_data."||".$form_Content, 0, 3600);
                
                
                //回复下一步操作提示
                
                $msgType = "text";
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "请输入备注，如奖金生效年月");
                echo $resultStr;
                exit;  
            }     
            //添加RMB第三步，备注
            if($last_do=="gl_tjRMB_1")
            {
                
                //用memcache保存这步操作
                $mc->set($fromUsername."_do", "gl_tjRMB_2", 0, 600);
                
                //用memcache保存这步数据，同时与上次数据合并，两个数据之间用||分割
                $mc->set($fromUsername."_data", $last_data."||".$form_Content, 0, 3600);
                
                list($roster_name,$rmb_rmb,$rmb_remark)=explode("||",$last_data."||".$form_Content);
                
                
                //回复下一步操作提示
                
                $msgType = "text";
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "添加人：".$roster_name."\n添加金额：".$rmb_rmb."元(人民币)\n备注：".$rmb_remark."\n确认添加输入“1”输入“0”返回主菜单！");
                echo $resultStr;
                exit;  
            }  
            //添加RMB第四步，确认添加
            if($last_do=="gl_tjRMB_2")
            {
                
                if(strtolower($form_Content)=="1")
                {
                    list($roster_name,$rmb_rmb,$rmb_remark)=explode("||",$last_data);
                    
                    //清空memcache动作
                    $mc->delete($fromUsername."_do");
                    
                    //清空memcache数据
                    $mc->delete($fromUsername."_data");
                    
                    //新增
                    $sql = "insert into rmb 
                               (rmb_name,rmb_rmb,rmb_addtime,rmb_remark)
                               values 
                               ('$roster_name','$rmb_rmb','$nowtime','$rmb_remark')";
                    
                    $mysql->runSql( $sql );
                    
                    if( $mysql->errno() != 0 )
                    {
                        $msgType = "text";
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "错误代码：".$mysql->errno());
                        echo $resultStr;
                        
                        exit;  
                    } 
                    else
                    {
                        $msgType = "text";
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "添加成功！");
                        echo $resultStr;
                        
                        exit;                         
                    }
                    
                    
                }
                else   
                {
                    //清空memcache动作
                    $mc->delete($fromUsername."_do");
                    
                    //清空memcache数据
                    $mc->delete($fromUsername."_data");
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "你已经退出当前操作，进行其他操作请输入“0”");
                    echo $resultStr;
                    exit;  
                }    
            }                      
            //积分兑换第一步，检查权限
            if((strtolower($form_Content)=="3")&&($last_do=="gl_0"))
            {

                $roster_name=$mysql->getVar("select roster_name
                    							   from roster
                                                   where roster_openid = '$fromUsername'and roster_K = 1");
                //无权限添加
                if(!$roster_name)
                {
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "对不起，您无此权限，输入“0”返回主菜单！");
                    echo $resultStr;
                    exit;  
                }
                else
                {
                    
                    //用memcache保存这步操作
                    $mc->set($fromUsername."_do", "gl_jfdh_0", 0, 600);
                    
                    //回复下一步操作提示                    
                    $back_str = "请输入兑换者的姓名或拼音首字母：（如“张三”请输入“ZS”）";
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, $back_str);
                    echo $resultStr;
                    exit;  
                    

                }
            }           

     
            //积分兑换第二步，兑换人
            if($last_do=="gl_jfdh_0")
            {

                $roster_name=$mysql->getVar("select roster_name
                    							   from roster
                                                   where roster_name='$form_Content' or roster_py='$form_Content'");          
                //无此员工
                if(!$roster_name)
                {
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "无此员工，请重新输入，或者输入“0”返回主菜单！");
                    echo $resultStr;
                    exit;  
                }
                else
                {
                    
                    //用memcache保存这步操作
                    $mc->set($fromUsername."_do", "gl_jfdh_1", 0, 600);
                    
                    //用memcache保存这步数据
                    $mc->set($fromUsername."_data", $roster_name, 0, 3600);
                    
                    //回复下一步操作提示                    
                    $back_str = "请输入要兑换的积分数：";
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, $back_str);
                    echo $resultStr;
                    exit;  
                    

                }
            }                   
 
            //积分兑换第三步，兑换分值
            if($last_do=="gl_jfdh_1")
            {
                
                //用memcache保存这步操作
                $mc->set($fromUsername."_do", "gl_jfdh_2", 0, 600);
                
                //用memcache保存这步数据，同时与上次数据合并，两个数据之间用||分割
                $mc->set($fromUsername."_data", $last_data."||".$form_Content, 0, 3600);
                
                
                //回复下一步操作提示
                
                $msgType = "text";
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "请输入兑换的奖金额（税后人民币）：");
                echo $resultStr;
                exit;  
            }                 
            
            //积分兑换第四步，RMB
            if($last_do=="gl_jfdh_2")
            {
                
                //用memcache保存这步操作
                $mc->set($fromUsername."_do", "gl_jfdh_3", 0, 600);
                
                //用memcache保存这步数据，同时与上次数据合并，两个数据之间用||分割
                $mc->set($fromUsername."_data", $last_data."||".$form_Content, 0, 3600);
                
                list($roster_name,$list_money,$rmb_rmb)=explode("||",$last_data."||".$form_Content); 
                                
                
                //回复下一步操作提示
                
                $msgType = "text";
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "兑换人：".$roster_name."\n兑换积分：".$list_money."分\n兑换奖金：".$rmb_rmb."元(人民币)\n确认添加输入“1”输入“0”返回主菜单！");
                echo $resultStr;
                exit;  
            }              
            
            //积分兑换第五步，确认添加
            if($last_do=="gl_jfdh_3")
            {
                
                if(strtolower($form_Content)=="1")
                {
                    list($roster_name,$list_money,$rmb_rmb)=explode("||",$last_data);
                    
                    //清空memcache动作
                    $mc->delete($fromUsername."_do");
                    
                    //清空memcache数据
                    $mc->delete($fromUsername."_data");
                    
                    $roster_name_cz=$mysql->getVar("select roster_name
                             from roster
                             where roster_openid = '$fromUsername'and roster_K = 1");                    
                    $rmb_remark = $roster_name."兑换积分（".$list_money."分）";   
                    //新增
                    $rmb_rmb = 0-$rmb_rmb;
                    $sql = "insert into rmb 
                               (rmb_name,rmb_rmb,rmb_addtime,rmb_remark)
                               values 
                               ('$roster_name_cz','$rmb_rmb','$nowtime','$rmb_remark' )";
                    
                    $mysql->runSql( $sql );
                    
                    $list_money = 0-$list_money;
                    $sql = "insert into list 
                               (list_name,list_code,list_money,list_addtime,list_status,list_checktime,list_checkname)
                               values 
                               ('$roster_name','K04','$list_money','$nowtime','1','$nowtime','$roster_name_cz' )";
                    $mysql->runSql( $sql );
                                        
                    
                    if( $mysql->errno() != 0 )
                    {
                        $msgType = "text";
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "错误代码：".$mysql->errno());
                        echo $resultStr;
                        
                        exit;  
                    } 
                    else
                    {
                        $msgType = "text";
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "添加成功！");
                        echo $resultStr;
                        
                        exit;                         
                    }
                    
                    
                }   
            }
            
            
            //绑定微信用户步骤开始
            
            //用户输入BD进行账号绑定
            if(strtolower($form_Content)=="bd")
            {
                //监测微信用户openid是否已经绑定
                
                if(check_user($fromUsername))
                {
                    
                    //提示已经绑定警告
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "你已经绑定账号，请不要重复操作！");
                    echo $resultStr;
                    exit;  
                }
                
                //用memcache保存这步操作，格式为名称、值、有效时间(单位秒)；
                $mc->set($fromUsername."_do", "bd_0", 0, 600);
                //回复绑定下一步操作提示
                $msgType = "text";
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "请输入你的姓名，输入“0”返回主菜单！");
                echo $resultStr;
                exit;  
            }
            
            //绑定第一步，输入姓名
            if($last_do=="bd_0")
            {
                //用memcache保存这步操作
                $mc->set($fromUsername."_do", "bd_1", 0, 600);
                
                //用memcache保存这步数据
                $mc->set($fromUsername."_data", $form_Content, 0, 3600);
                
                //回复绑定下一步操作提示
                $msgType = "text";
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "请输入你的工号，输入“0”返回主菜单！");
                echo $resultStr;
                exit;  
            }
            
            //绑定第二步，输入工号
            if($last_do=="bd_1")
            {
                //用memcache保存这步操作
                $mc->set($fromUsername."_do", "bd_2", 0, 600);
                
                //用memcache保存这步数据，同时与上次数据合并，两个数据之间用||分割
                $mc->set($fromUsername."_data", $last_data."||".$form_Content, 0, 3600);
                
                //回复绑定下一步操作提示
                $msgType = "text";
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "请输入你的手机号码，输入“0”返回主菜单！");
                echo $resultStr;
                exit;  
            }
            //绑定第三步，输入手机号码
            if($last_do=="bd_2")
            {
                //清空memcache动作
                $mc->delete($fromUsername."_do");
                
                //清空memcache数据
                $mc->delete($fromUsername."_data");
                
                //将之前保存数据变为数组；
                list($roster_name,$roster_number,$roster_mp)=explode("||",$last_data."||".$form_Content);
                
                //判断资料是否完全吻合，并且openid为空即未绑定状态
                
                $roster_data=$mysql->getLine("select roster_id from roster where 
                   roster_name='$roster_name' and roster_number='$roster_number' and 
                   roster_mp='$roster_mp' and roster_openid='' and status=1");
                
                //资料一致确认绑定
                
                if($roster_data)
                {
                    //将用户openid与该记录绑定
                    $roster_id=$roster_data["roster_id"];
                    $sql = "update roster set roster_openid='$fromUsername' where roster_id=$roster_id";
                    $mysql->runSql( $sql );
                    if( $mysql->errno() != 0 )
                    {
                        $back_str="绑定失败，可能是资料不正确或者该账号已经绑定，请重新输入BD进行绑定！";
                        
                    }
                    else
                    {
                        $back_str="绑定成功！请输入“0”选择操作";
                    }
                    
                }
                else
                {
                    $back_str="绑定失败，可能是资料不正确或者该账号已经绑定，请重新输入BD进行绑定！";
                }
                
                //回复绑定下一步操作提示
                $msgType = "text";
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType,  $back_str);
                echo $resultStr;
                exit;  
            }
            //绑定用户操作结束
            
            //用户自动回复
            $msgType = "text";
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType,  "无法识别你的指令，需求帮助请输入 0 ");
            echo $resultStr;
            exit;  
            
        }
    }
    
}
else 
{
    echo "";
    exit;
}

//检测微信用户是否绑定
function check_user($fromUsername)
{
    //定义全局变量
    global $mysql;
    
    $roster_value=$mysql->getLine("select * from roster where roster_openid='$fromUsername'");
    //如果没有绑定
    if(!$roster_value)
    {
        
        return false;
    }
    //如果已经绑定（误取消重新关注员工）
    else
    {
        return $roster_value;        
    }
    
}
?>